/**
 * 工具函数
 */
// 纯数字验证
export const isNum = (val) => {
  return /^[0-9]*$/.test(val)
}
// 防抖
export const debounce = (() => {
  let timer = null
  return (callback, wait) => {
    clearTimeout(timer)
    timer = setTimeout(callback, wait)
  }
})()

// 转换时间格式 年-月-日
export const padZero = (n) => {
  return n < 10 ? '0' + n : n
}

export const normalizeDate = (time, s = '-') => {
  const date = new Date(time)

  const y = date.getFullYear()
  const m = date.getMonth() + 1
  const d = date.getDate()

  return [y, m, d].map(v => padZero(v)).join(s)
}

// 转换时间格式 年-月-日 时：分：秒
export const format = (shijianchuo) => {
  const time = new Date(shijianchuo)
  const y = time.getFullYear()
  const m = time.getMonth() + 1
  const d = time.getDate()
  const h = time.getHours()
  const mm = time.getMinutes()
  const s = time.getSeconds()
  return y + '-' + padZero(m) + '-' + padZero(d) + ' ' + padZero(h) + ':' + padZero(mm) + ':' + padZero(s)
}
